﻿Imports System.Net
Imports System.IO

Public Class Form2
    Public MainForm As Form1
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtcon.Text = My.Settings.con
        txtfileprotocol.Text = My.Settings.fileprotocol
        lbluser.Text = My.Settings.username
        Label5.Text = Me.ProductVersion
        txtftpusername.Text = My.Settings.ftpusername
        txtftppassword.Text = My.Settings.ftppassword
        If (My.Settings.username = "") Then
            Button4.Text = "Sign in"
        Else
            Button4.Text = "Sign Out"
        End If

        Dim getversion As New Threading.Thread(AddressOf CheckUpdate)
        getversion.Start()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        My.Settings.con = txtcon.Text
        My.Settings.fileprotocol = txtfileprotocol.Text
        My.Settings.ftpusername = txtftpusername.Text
        My.Settings.ftppassword = txtftppassword.Text
        My.Settings.Save()

        Me.Close()
        Dim getversion As New Threading.Thread(AddressOf CheckUpdate)
        getversion.Start()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Dim con As New SqlConnection
        Dim con As New Odbc.OdbcConnection

        'Dim MyConString As String = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=192.168.1.19; DATABASE=documentmanagementwamp; UID=SERGENTLOIS; PASSWORD=1;"
        'LOCAL Data Source=LOIS-PC\SQLEXPRESS;Initial Catalog=DocumentManagement;Integrated Security=True

        Try
            con.ConnectionString = txtcon.Text
            'con.ConnectionString = MyConString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


        Try
            con.Open()
            MsgBox("Test Connection Success!", MsgBoxStyle.Information)
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If Button4.Text = "Sign Out" Then
            My.Settings.username = ""
            My.Settings.password = ""
            My.Settings.email = ""
            My.Settings.company = ""
            My.Settings.department = ""
            My.Settings.role = ""
            MainForm.LoadItems()
            Me.Close()
        Else
            Me.Hide()
            LoginForm1.MainForm = MainForm
            LoginForm1.ShowDialog()
            MainForm.LoadItems()
        End If

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            'save settings
            Dim objWriter As New System.IO.StreamWriter("default.txt")
            Dim defaultsettings As String = "Connection_String|" & My.Settings.con & vbNewLine & _
            "File_Protocol|" & My.Settings.fileprotocol & vbNewLine & _
            "FTP_Username|" & My.Settings.ftpusername & vbNewLine & _
            "FTP_Password|" & My.Settings.ftppassword

            objWriter.Write(defaultsettings)
            objWriter.Close()
            Shell("updater.exe -" & My.Settings.fileprotocol & "-" & My.Settings.ftpusername & "-" & My.Settings.ftppassword & vbCrLf, AppWinStyle.NormalFocus)
            Application.Exit()
            End
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub CheckUpdate()
        'Check for update
        Try
            Button3.Text = "Checking.."
            If Not My.Settings.fileprotocol.Contains("/") Then
                My.Settings.fileprotocol = My.Settings.fileprotocol & "/"
            End If
            If Not My.Settings.fileprotocol.Contains("ftp://") Then
                My.Settings.fileprotocol = "ftp://" & My.Settings.fileprotocol
            End If
            Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create(My.Settings.fileprotocol & "updates/updates.txt"), FtpWebRequest)

            ftp.Credentials = New NetworkCredential(My.Settings.ftpusername, My.Settings.ftppassword)
            ftp.KeepAlive = False
            ftp.UseBinary = True
            ftp.Method = WebRequestMethods.Ftp.DownloadFile

            Using FtpResponse As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
                Using ResponseStream As IO.Stream = FtpResponse.GetResponseStream

                    If File.Exists("filemanagerupdate.txt") Then
                        File.Delete("filemanagerupdate.txt")
                    End If
                    Using fs As New IO.FileStream("filemanagerupdate.txt", FileMode.Create)
                        Dim buffer(2047) As Byte
                        Dim read As Integer = 0
                        Do
                            read = ResponseStream.Read(buffer, 0, buffer.Length)
                            fs.Write(buffer, 0, read)
                            Console.Write(".")
                        Loop Until read = 0
                        ResponseStream.Close()
                        fs.Flush()
                        fs.Close()
                    End Using
                    ResponseStream.Close()
                End Using
            End Using


            Dim value As String = File.ReadAllText("filemanagerupdate.txt")

            If value > Me.ProductVersion Then
                'MsgBox("Update Available [" & value & "]")
                Button3.Text = "Update [" & value & "]"
                Button3.Enabled = True
            ElseIf value = Me.ProductVersion Then
                Button3.Text = "Updated!"
                Button3.Enabled = False
            Else
                Button3.Text = "Unvailable"
                Button3.Enabled = False
            End If
        Catch ex As Exception
            Button3.Text = "Unvailable"
            Button3.Enabled = False

        End Try

    End Sub

End Class